#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: Comzyh
# @Date:   2014-10-26 18:35:19
# @Last Modified by:   Comzyh
# @Last Modified time: 2014-10-26 20:15:29
import os
def main():
	def total_score(arr):
		return arr[0]*5 + arr[1]*4 + arr[2]*2 + arr[3]*1;
	scores_dir = os.path.abspath(os.curdir)
	team_score = {}
	for filename in os.listdir(scores_dir):
		if filename.endswith(".log"):
			score_file = open(os.path.abspath(filename),'r')
			game = []
			for line in score_file.readlines():
				[team,score] = line.split("\t")
				score = float(score)
				game.append({'team':team,'score':score})
			game.sort(key = lambda x:x,reverse = True)
			for i in range(4):
				if not team_score.has_key(game[i]['team']):
					team_score[game[i]['team']] = {
					'team':game[i]['team'],
					'rank':[0,0,0,0],
					}
				team_score[game[i]['team']]['rank'][i] += 1
			score_file.close()
	result = team_score.values()
	for r in result:
		r['score'] = total_score(r['rank'])
	result.sort(key = lambda x:x['score'],reverse = True)

	result_file = open('result.txt','w')
	for r in result:
		#result_file.write("%s\t\t%s\t\t(%d,%d,%d,%d)\n" % (r['team'],r['score'],r['rank'][0],r['rank'][1],r['rank'][2],r['rank'][3]))
		result_file.write("%s\t\t%s\t\t%s\n" % (r['team'],r['score'],r['rank']))
	result_file.close()

if __name__ == '__main__':
	main()
